#include "musicform1.h"
#include "ui_musicform1.h"
#include "head.h"

MusicForm1::MusicForm1(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicForm1),
    pModelForm1(new QStandardItemModel(0, 5, this)) // 初始化模型，行数设置为0，列数为5（多了一列用于显示收藏状态）
{
    ui->setupUi(this);

    // 设置数据模型的表头
    QStringList list1 = QStringList() << "id" << "歌名" << "专辑" << "歌手" << "时长" << "收藏状态";
    pModelForm1->setHorizontalHeaderLabels(list1); // 设置表头
    ui->tableViewF1->setModel(pModelForm1);

    // 设置定时器每5秒刷新一次
    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &MusicForm1::loadFavoriteSongs);
    timer->start(500); // 每5秒刷新

    // 初始加载数据
    loadFavoriteSongs();

    ui->tableViewF1->verticalHeader()->setVisible(false);

    playlistF1 = new QMediaPlaylist;
    playlistF1->setCurrentIndex(1);

    playerF1 = new QMediaPlayer;
    playerF1->setPlaylist(playlistF1);

    //创建一个网络访问管理对象
    managerF1 = new QNetworkAccessManager(this);
    connect(managerF1, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(replyFinished(QNetworkReply*)));


//    misicIdListF1.push_back(id);  //存放歌曲id
//    playlistF1->addMedia(QUrl(QString("http://music.163.com/song/media/outer/url?id=%1.mp3").arg(id)));
}

void MusicForm1::replyFinished(QNetworkReply* reply)
{
    qDebug()<<"--------\"hello\"--------"<<reply->attribute(QNetworkRequest::HttpStatusCodeAttribute)<<endl;
//    QNetworkRequest::HttpStatusCodeAttribute网址返回的状态码
    if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200)
    {
        qDebug()<<"访问服务器成功"<<endl;

    }
}

void MusicForm1::loadFavoriteSongs() {
    // 清空模型
    pModelForm1->clear();
    // 重新设置表头
    QStringList list1 = QStringList() << "id" << "歌名" << "专辑" << "歌手" << "时长" << "收藏状态";
    pModelForm1->setHorizontalHeaderLabels(list1);

    // 初始化数据库查询
    QSqlQuery query;

    // SQL 查询，提取收藏状态
    QString sql = "SELECT id , song_name, album, singer, duration, favorite FROM music";
    if (query.exec(sql)) {
        int row = 0;
        while (query.next()) {
            QString songId=query.value(0).toString();
            QString songName = query.value(1).toString();
            QString albumName = query.value(2).toString();
            QString singerName = query.value(3).toString();
            QString duration = query.value(4).toString();
            QString favoriteStatus = query.value(5).toString(); // 收藏状态

            // 将数据填充到模型中
            pModelForm1->setItem(row, 0, new QStandardItem(songId));
            pModelForm1->setItem(row, 1, new QStandardItem(songName));
            pModelForm1->setItem(row, 2, new QStandardItem(albumName));
            pModelForm1->setItem(row, 3, new QStandardItem(singerName));
            pModelForm1->setItem(row, 4, new QStandardItem(duration));
            pModelForm1->setItem(row, 5, new QStandardItem(favoriteStatus));

            row++;
        }
    } else {
        qDebug() << "数据库查询失败: " << query.lastError();
    }
}
void MusicForm1::mouseDoubleClickEvent(QMouseEvent *event) {
    // 获取鼠标双击时的坐标
    QPoint pos = event->pos();

    // 获取当前被选中的索引
    QModelIndex index = ui->tableViewF1->indexAt(pos);

    // 确保选中的是有效索引
    if (index.isValid()) {
        // 获取歌曲ID
        int songID = getSongId(index);

        // 生成歌曲播放链接
        QString url = QString("http://music.163.com/song/media/outer/url?id=%1.mp3").arg(songID);
        misicIdListF1.push_back(songID);  //存放歌曲id
        playlistF1->addMedia(QUrl(QString("http://music.163.com/song/media/outer/url?id=%1.mp3").arg(songID)));

        // 播放音乐，使用QDesktopServices打开链接
        QDesktopServices::openUrl(QUrl(url));
    }
}

int MusicForm1::getSongId(const QModelIndex &index) {
    // 假设ID在数据库的第一列，使用 index.row() 和 column 0 获取ID
    return pModelForm1->data(pModelForm1->index(index.row(), 0)).toInt(); // 假设ID在第一列
}

MusicForm1::~MusicForm1()
{
    delete ui;
}
